package com.hertz.appr.service.timer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

import com.hertz.appr.pojo.db.Action;
import com.hertz.appr.pojo.db.Task;
import com.hertz.appr.service.ActionService;
import com.hertz.appr.service.ResultTemplateService;
import com.hertz.appr.service.TaskService;
import com.hertz.appr.util.Configuration;


public class PublishActiveAction {
	private ActionService actionService;
	private TaskService taskService;
	private ResultTemplateService resultTemplateService;
	private Logger log = Logger.getLogger(PublishActiveAction.class);
	private String provider;
	public void buildPublishActionActionTask(){
		log.info("start build publish active action task");
		List<Action> list = actionService.getActions(provider, Action.ACTION_STATUS_START);
		if(list == null || list.size() == 0){
			log.info("no active action");
		}else{
			Integer size = list.size();
			Random random = new Random();
			int index = random.nextInt(size);
			if(index <= size){
				Action action = list.get(index);
				log.info("public action");
				log.info(action);
				Task task = new Task();
				task.setActionId(action.getId());
				task.setCreateTime(System.currentTimeMillis());
				task.setProvider(provider);
				task.setSourceTid(action.getActionTid());
				task.setStatus(Task.TASK_STATUS_WAIT);
				task.setSystemTime(System.currentTimeMillis());
				task.setType(Task.TASK_TYPE_PUBLICACTION);
				String msg = ""	;
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("aidStr", ActionService.getActionId62Hex(action.getId()));
				data.put("startTime", action.getStartTimeStr());
				data.put("tUrl", Configuration.getProperty(provider + ".t.baseurl") + action.getActionTid());
				data.put("uid", action.getUid());
				data.put("nick", action.getNick());
				msg = resultTemplateService.getTemplateContent("publishActiveAction.ftl", data);
				task.setMsg(msg);
				taskService.save(task);
			}
		}
		log.info("end publish active action");
	}
	public ActionService getActionService() {
		return actionService;
	}
	public void setActionService(ActionService actionService) {
		this.actionService = actionService;
	}
	public TaskService getTaskService() {
		return taskService;
	}
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	public ResultTemplateService getResultTemplateService() {
		return resultTemplateService;
	}
	public void setResultTemplateService(ResultTemplateService resultTemplateService) {
		this.resultTemplateService = resultTemplateService;
	}
	public String getProvider() {
		return provider;
	}
	public void setProvider(String provider) {
		this.provider = provider;
	}
	
	
}
